package com.sskj.xyft;


import android.content.Context;
import android.content.Intent;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.sskj.base.BuildConfig;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xyft.R;
import com.sskj.xyft.message.data.MessageData;
import com.sskj.xyft.message.data.NewsDetail;
import com.sskj.xyft.message.data.NoticeDetail;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/26
 */
public class WebActivity extends MVPActivity<WebPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.webView)
    WebView webView;
    AgentWeb mAgentWeb;
    private String url;
    private int type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public WebPresenter getPresenter() {
        return new WebPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());
        url = getIntent().getStringExtra("url");
        type = getIntent().getIntExtra("type", 0);
    }

    @Override
    public void initData() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(webView, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go("");
        //资讯详情
        if (type == 0) {
            toolbar.setTitle("资讯详情");
            mPresenter.getNewsDetail(url);
        } else if (type == 1) {
            //公告详情
            toolbar.setTitle("公告详情");
            mPresenter.getNoticeDetail(url);
        } else if (type == 2) {
            //新闻详情
            toolbar.setTitle("新闻详情");
            mPresenter.getAppNewsDetail(url);
        }

    }


    public String formatImg(String content) {
        Document document = Jsoup.parse(content);
        Elements imgs = document.getElementsByTag("img");
        for (int i = 0; i < imgs.size(); i++) {
            Element img = imgs.get(i);
            img.attr("width", "100%").attr("height", "auto");
//            String url = img.attr("src");
//            if (!url.contains("http")) {
//                url = "http://newsapi.eastmoney.com/";
//            }
//            img.attr("src", url);
        }
        return document.toString();
    }

    public static void start(Context context, String url, int type) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("type", type);

        context.startActivity(intent);
    }


    public void setNewsDetail(NewsDetail data) {
        String content = data.getNews().getBody();
        mAgentWeb.getWebCreator().getWebView().loadDataWithBaseURL("http://newsapi.eastmoney.com/", formatImg(content), "text/html", "utf-8", null);
    }

    public void setNoticeDetail(NoticeDetail data) {
        String content = data.getContent();
        mAgentWeb.getWebCreator().getWebView().loadDataWithBaseURL(BuildConfig.BASE_URL, formatImg(content), "text/html", "utf-8", null);
    }

    public void setAppNewsDetail(MessageData data) {
        if (data != null && data.getData() != null) {
            String content = data.getData().get(0).getDetails();
            mAgentWeb.getWebCreator().getWebView().loadDataWithBaseURL(BuildConfig.BASE_URL, formatImg(content), "text/html", "utf-8", null);
        }
    }
}
